package com.zhenyi.liuxue.core;

import com.cjw.model.DBMap;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.utils.PathUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class TemplateUtils {

	private final String SUBSCRIBE_TEMPLATE_ID_GZH = PropKit.get("LX_SUBSCRIBE_TEMPLATE_ID_GZH");
	private final String APP_ID_GZH = PropKit.get("LX_APP_ID_GZH", "wx07ccb1a6ea13f727");// "wx07ccb1a6ea13f727";

	private final String SUBSCRIBE_TEMPLATE_ID = PropKit.get("LX_SUBSCRIBE_TEMPLATE_ID");
	private final String APP_ID = PropKit.get("LX_APP_ID", "wx7ca6625ef7867aa7");
	private final String APP_SECRET = PropKit.get("LX_APP_SECRET", "aa59a2e17a1747821a9387f5677ee4e1");
	private final String ACCESS_TOKEN_KEY = APP_ID + "_AccessToken";
	private final String URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}";
	private final String UNIFORM_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=";
	private final String TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
	private final String TEMPLATE_GZH_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

	private final String TEMPLATE_ID_VERIFY_PASSED = "OuC1iNM79f3Ae87j-ACjw-3B668JzUrchPHm5bSaBaI";
	private final String TEMPLATE_ID_VERIFY_FAILED = "oC2EFMQ5VK7Hc_QdHvAUnP183nBwxm77gZ7WXd5vjxY";

	private final Cache cache = Redis.use();

	public String getAccessToken() {
		String token = cache.get(ACCESS_TOKEN_KEY);
		if (StrKit.notBlank(token)) {
			System.out.println("ret");
			return token;
		}
		String url = StrFormatter.format(URL, APP_ID, APP_SECRET);
		String response = HttpKit.get(url);
		System.out.println(response);
		JSONObject jsonObject = JSONUtil.parseObj(response);
		String access_token = jsonObject.getStr("access_token");
		int expires_in = jsonObject.getInt("expires_in");
		// int errcode = jsonObject.getInt("errcode");
		// if (errcode != 0) {
		// System.out.println(response);
		// return null;
		// }
		cache.setex(ACCESS_TOKEN_KEY, expires_in, access_token);
		return access_token;
	}

	public void send(String openid, String firstStr, String keyword1Str, String keyword2Str, String keyword3Str,
			String remarkStr, String pagepath) {
		String accessToken = getAccessToken();
		DBMap map = new DBMap();

		String touser = openid;
		DBMap mp_template_msg = new DBMap();

		DBMap miniprogram = new DBMap();
		miniprogram.put("appid", APP_ID);
		miniprogram.put("pagepath", pagepath);

		DBMap data = new DBMap();
		DBMap first = new DBMap();
		first.put("color", "#173177");
		first.put("value", firstStr);
		DBMap keyword1 = new DBMap();
		keyword1.put("color", "#173177");
		keyword1.put("value", keyword1Str);
		DBMap keyword2 = new DBMap();
		keyword2.put("color", "#173177");
		keyword2.put("value", keyword2Str);
		DBMap keyword3 = new DBMap();
		keyword3.put("color", "#173177");
		keyword3.put("value", keyword3Str);
		DBMap remark = new DBMap();
		remark.put("color", "#173177");
		remark.put("value", remarkStr);

		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword3);
		data.put("remark", remark);
		data.put("first", first);

		mp_template_msg.put("appid", APP_ID_GZH);
		mp_template_msg.put("template_id", SUBSCRIBE_TEMPLATE_ID_GZH);
		mp_template_msg.put("url", "http://weixin.qq.com/download");
		mp_template_msg.put("miniprogram", miniprogram);
		mp_template_msg.put("data", data);

		map.put("touser", touser);
		map.put("mp_template_msg", mp_template_msg);
		map.put("touser", touser);

		String json = JsonKit.toJson(map);
		String url = UNIFORM_URL + accessToken;
		String path = PathUtils.getFile("logs", "superlink", "push", DateUtil.today(), "push.txt");
		FileWriter fileWriter = new FileWriter(path);
		String result = HttpUtil.post(url, json);
		fileWriter.append(DateUtil.now() + " " + openid + " " + keyword1Str + " " + result + "\n");
	}

	public void send() {
		String accessToken = getAccessToken();
		DBMap map = new DBMap();

		String touser = "o0OpG46xEhm8OcilXW5gRYTwTqYY";
		DBMap mp_template_msg = new DBMap();

		DBMap miniprogram = new DBMap();
		miniprogram.put("appid", APP_ID);
		miniprogram.put("pagepath", "pages/app/schools/index/index");

		DBMap data = new DBMap();
		DBMap keyword1 = new DBMap();
		keyword1.put("color", "#173177");
		keyword1.put("value", "业务标题");
		DBMap keyword2 = new DBMap();
		keyword2.put("color", "#173177");
		keyword2.put("value", "业务内容");
		DBMap keyword3 = new DBMap();
		keyword3.put("color", "#173177");
		keyword3.put("value", "keyword3");
		DBMap first = new DBMap();
		first.put("color", "#173177");
		first.put("value", "first");
		DBMap remark = new DBMap();
		remark.put("color", "#173177");
		remark.put("value", "remark");

		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword2);
		data.put("remark", remark);
		data.put("first", first);

		mp_template_msg.put("appid", APP_ID_GZH);
		mp_template_msg.put("template_id", SUBSCRIBE_TEMPLATE_ID_GZH);
		mp_template_msg.put("url", "http://weixin.qq.com/download");
		mp_template_msg.put("miniprogram", miniprogram);
		mp_template_msg.put("data", data);

		map.put("touser", touser);
		map.put("mp_template_msg", mp_template_msg);
		map.put("touser", touser);

		String json = JsonKit.toJson(map);
		System.out.println(json);
		String url = UNIFORM_URL + accessToken;
		System.out.println(HttpUtil.post(url, json));
	}

	public void sendVerifyPassed(String openId, String formId, String userName, String time) {
		sendTemplate(openId, formId, TEMPLATE_ID_VERIFY_PASSED, "pages/app/schools/index/index", userName, "通过",
				"现在可以使用“超级留学顾问”完整功能，第一时间获取申请信息！", time);
	}

	public void sendVeirfyFailed(String openId, String formId, String userName, String reason, String time) {
		sendTemplate(openId, formId, TEMPLATE_ID_VERIFY_FAILED, "pages/app/schools/index/index", userName,
				"您在“超级留学顾问”注册的账号未通过审核，请重新提交相关材料。", reason, time);
	}

	public void sendTemplate(String openId, String formId, String templateId, String pagePath, String keyword1Str,
			String keyword2Str, String keyword3Str, String keyword4Str) {

		String accessToken = getAccessToken();
		DBMap map = new DBMap();

		DBMap data = new DBMap();
		DBMap keyword1 = new DBMap();
		keyword1.put("value", keyword1Str);
		DBMap keyword2 = new DBMap();
		keyword2.put("value", keyword2Str);
		DBMap keyword3 = new DBMap();
		keyword3.put("value", keyword3Str);
		DBMap keyword4 = new DBMap();
		keyword4.put("value", keyword4Str);

		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword3);
		data.put("keyword4", keyword4);

		map.put("touser", openId);
		map.put("template_id", templateId);
		map.put("page", pagePath);
		map.put("form_id", formId);
		map.put("data", data);

		String json = JsonKit.toJson(map);
		System.out.println(json);
		String url = TEMPLATE_URL + accessToken;
		System.out.println(HttpUtil.post(url, json));
	}

	public void sendActivityStartNotification(String openId, String templateId, String link, String keyword1Str,
			String keyword2Str, String keyword3Str, String firstStr, String remarkStr) {
		String accessToken = getAccessToken();
		DBMap map = new DBMap();

		DBMap data = new DBMap();
		DBMap keyword1 = new DBMap();
		keyword1.put("value", keyword1Str);
		DBMap keyword2 = new DBMap();
		keyword2.put("value", keyword2Str);
		DBMap keyword3 = new DBMap();
		keyword3.put("value", keyword3Str);
		DBMap first = new DBMap();
		first.put("value", firstStr);
		DBMap remark = new DBMap();
		remark.put("value", remarkStr);

		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword3);
		data.put("first", first);
		data.put("remark", remark);

		map.put("touser", openId);
		map.put("template_id", templateId);
		map.put("url", link);
		map.put("data", data);

		String json = JsonKit.toJson(map);
		System.out.println(json);
		String url = TEMPLATE_GZH_URL + accessToken;
		System.out.println(HttpUtil.post(url, json));
	}
}
